package cc.shacocloud.redis.study.util.redis;

import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;

import java.util.StringJoiner;

/**
 * Redis 键工具，提供一种可视化度更高的键拼接
 */
@Slf4j
public class RedisKeys {

    /**
     * redis 键生成前缀
     */
    private final String PREFIX;

    /**
     * redis 键的分隔符
     */
    public String DELIMITER = ":";


    public RedisKeys(@NotNull String... prefix) {
        // 设置前缀
        this.PREFIX = merge(null, this.DELIMITER, prefix);
    }


    /**
     * 生成redis的键
     *
     * @param keys 可以一个 也可以多个，之间使用{@linkplain #DELIMITER}拼接
     * @return 键
     */
    @NotNull
    public String keys(@NotNull String... keys) {
        return merge(this.PREFIX, this.DELIMITER, keys);
    }

    private String merge(String prefix, @NotNull String delimiter, String... keys) {
        StringJoiner joiner = new StringJoiner(delimiter);

        if (prefix != null) joiner.add(prefix);

        if (keys != null)
            for (String key : keys) {
                joiner.add(key);
            }

        return joiner.toString();
    }

}
